import React, {PureComponent} from 'react';
import {View, Image, StyleSheet, Text} from 'react-native';
import {updateUserInfo} from '../../redux/action';
import {connect} from 'react-redux';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#160231',
    display: 'flex',
    alignItems: 'center',
  },
  title: {
    fontSize: 36,
    color: 'white',
    marginTop: 200,
    fontWeight: 'bold',
  },
});

class Applaunch extends PureComponent {
  async componentDidMount() {
    //更新token信息
    await this.props.updateUserInfo();

    //根据本地存储的token查询用户信息，如果过期或者不存在则跳转Login
    if (this.props.userInfo && this.props.userInfo.identify.length != 0) {
      this.props.navigation.navigate('Home');
    } else {
      this.props.navigation.navigate('Login');
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <Image
          style={{marginTop: 300, height: 56, width: 192}}
          source={require('../../assets/login/logo.png')}></Image>
        <Text
          style={{
            marginTop: 50,
            width: '100%',
            textAlign: 'center',
            color: 'white',
          }}>
          signin everything
        </Text>
      </View>
    );
  }
}

const mapStateToProps = (state) => ({
  userInfo: state.User.userInfo,
});

const mapDispatchToProps = (dispatch) => ({
  updateUserInfo: (item) => dispatch(updateUserInfo(item)),
});

export default connect(mapStateToProps, mapDispatchToProps)(Applaunch);
